Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Domain.ValueObjects
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Services

Public Class ShowGroup

    Private app As New PgdApp()

    Public Sub New(ByVal groupId As Guid)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        Dim groupRepository As New GroupRepository()
        Dim group As Group = groupRepository.GetById(groupId)

        Me.Id = group.Id
        Me.FormTitle = group.Name

        Dim permissions = app.Permissions.GetAll()
        Dim groupPermissions As New List(Of PermissionInfo)

        For Each p As Permission In permissions

            If group.HasPermission(p.Id) Then
                Dim isDeletable As Boolean = group.IsPermissionDeletable(p.Id)
                groupPermissions.Add(New PermissionInfo(p.Id, p.Name, True, isDeletable))
            Else
                groupPermissions.Add(New PermissionInfo(p.Id, p.Name, False, True))
            End If
        Next

        Me.AddGroupPermissions(groupPermissions)

        'Load Users Tab
        Me.LoadUsersTab()

    End Sub

    Private Sub LoadUsersTab()

        Dim form As New ManageGroupUsers(Me.Id)
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.LoadExistingTabPage(Me.TabContainer, form, GroupTabs.ManageUsers)

        form.Show()

    End Sub

    Private Sub AddGroupPermissions(ByVal permissions As List(Of PermissionInfo))

        Me.GroupPermssions.DisplayMember = "Name"

        For Each p As PermissionInfo In permissions
            Dim state As CheckState = Me.GetCheckBoxState(p)
            Me.GroupPermssions.Items.Add(p, state)
        Next

    End Sub

    Private Function GetCheckBoxState(ByVal pi As PermissionInfo) As CheckState

        If pi.IsDeletable Then
            If pi.Checked Then
                Return CheckState.Checked
            Else
                Return CheckState.Unchecked
            End If
        Else
            Return CheckState.Indeterminate
        End If

    End Function

#Region "IShowGroupView"

    Private _id As Guid
    Public Property Id() As Guid
        Get
            Return _id
        End Get
        Set(ByVal value As Guid)
            _id = value
        End Set
    End Property

    Public ReadOnly Property GroupPermssions() As CheckedListBox
        Get
            Return clbGroupPermssions
        End Get
    End Property

    Public Property TabContainer() As TabControl
        Get
            Return MainTabContainer
        End Get
        Set(ByVal value As TabControl)
            MainTabContainer = value
        End Set
    End Property

    Public Property FormTitle() As String
        Get
            Return txtFormTitle.Text
        End Get
        Set(ByVal value As String)
            txtFormTitle.Text = value
        End Set
    End Property

#End Region

    Private Sub clbGroupPermssions_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clbGroupPermssions.ItemCheck
        If clbGroupPermssions.SelectedItem IsNot Nothing Then
            Dim permissionId As Guid = CType(clbGroupPermssions.SelectedItem, PermissionInfo).Id

            If e.CurrentValue <> CheckState.Indeterminate Then
                If e.NewValue = CheckState.Checked Then
                    Me.AddIndividualPermission(permissionId)
                Else
                    Me.RemoveIndividualPermission(permissionId)
                End If
            Else
                e.NewValue = e.CurrentValue
            End If
        End If
    End Sub

    Public Sub AddIndividualPermission(ByVal permissionId As Guid)

        Dim groupRepository As New GroupRepository()
        Dim groupId As Guid = Me.Id

        Dim group As Group = groupRepository.GetById(groupId)

        Dim permission = app.Permissions.GetById(permissionId)

        groupRepository.AddPermission(group, permission, True)

        groupRepository.SaveOrUpdate(group)

    End Sub

    Public Sub RemoveIndividualPermission(ByVal permissionId As Guid)


        Dim groupRepository As New GroupRepository()
        Dim groupId As Guid = Me.Id

        Dim group As Group = groupRepository.GetById(groupId)

        Dim permission = app.Permissions.GetById(permissionId)

        groupRepository.RemovePermission(group, permission)

        groupRepository.SaveOrUpdate(group)

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class